﻿using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using bGallery.Core.Data;
using bGallery.Core.Domain;
using bGallery.DataSource;

namespace bGallery.DataSource
{
	/// <summary>
	/// Data source for category tree
	/// </summary>
	public class CategoryTreeDataSource : ICategoryTreeDataSource
	{
		private IList<Category> categories;
		private Category rootCategory;
		private readonly ICategoryDao categoryDao;

		/// <summary>
		/// Sets root category
		/// </summary>
		public Category RootCategory
		{
			set { rootCategory = value; }
		}

		/// <summary>
		/// Creates new CategoryTreeDataSource instance
		/// </summary>
		/// <param name="categoryDao"></param>
        public CategoryTreeDataSource(ICategoryDao categoryDao)
        {
			this.categoryDao = categoryDao;
            categories = categoryDao.GetTree(null);
        }

		/// <summary>
		/// Creates new CategoryTreeDataSource instance
		/// </summary>
		/// <param name="categories"></param>
        public CategoryTreeDataSource(IList<Category> categories)
		{
			this.categories = categories;
		}

		/// <summary>
		/// Updates data in data source
		/// </summary>
		public void Update()
		{
			categories = categoryDao.GetTree(rootCategory);
		}

		/// <summary>
		/// Get hierarchy data
		/// </summary>
		/// <param name="enumeratedItem"></param>
		/// <returns></returns>
		public IHierarchyData GetHierarchyData(object enumeratedItem)
		{
			return new CategoryData(enumeratedItem as Category);
		}

		/// <summary>
		/// Gets enumerator
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return categories.GetEnumerator();
		}
	}
}